const borsh = require('borsh');
class MessageData {
    constructor(_message_type, _nft_contract, _nft_id, _price_token, _price, _recipent, _buyer) {
        this.data ={
            message_type:_message_type,
            nft_contract:_nft_contract,
            nft_id:_nft_id,
            price_token:_price_token,
            price:_price,
            recipent:_recipent,
            buyer:_buyer
        }

        this.schema = {
            struct: {
                message_type: 'string',
                nft_contract: 'string',
                nft_id: 'u128',
                price_token: 'string',
                price: 'u128',
                recipent: 'string',
                buyer: 'string',
            }
        }
    }
    toBorshBytes() {
        return borsh.serialize(this.schema, this.data);
    }
    fromBorshBytes(encoded) {
        return borsh.deserialize(this.schema, encoded);
    }
}

class FunctionCallData {
    constructor(messageFunc, messageData) {
        this.schema = {
            struct: {
                messageFunc: 'string',
                messageData: {array: {type: 'u8'}}
            }
        };
        this.data = {
            messageFunc:messageFunc,
            messageData:messageData,
        }
    }
    toBorshBytes() {
        return borsh.serialize(this.schema, this.data);
    }
    fromBorshBytes(encoded) {
        return borsh.deserialize(this.schema, encoded);
    }

}

class InboundFunctionCallData {
    constructor(functionCallData, sourceChainId, sourceContract) {
        this.data ={
            functionCallData:functionCallData,
            sourceChainId:sourceChainId,
            sourceContract:sourceContract,

        }
        this.schema = {
            struct:{
                functionCallData:{array:{type:'u8'}},
                sourceChainId:'u128',
                sourceContract:"string",

            }

        }
    }
    toBorshBytes() {
        return borsh.serialize(this.schema, this.data);
    }
    fromBorshBytes(encoded) {
        return borsh.deserialize(this.schema, encoded);
    }
}

class EncodeInfo{
    constructor(_inboundFunctionCallData, _taskId, _networkId, _contractAddr) {
        this.schema = {
            struct:{
                taskId: {array: {type: 'u8'}},
                networkId: 'u128',
                contractAddr: {array: {type: 'u8',len:32}},
                inboundFunctionCallData: {array: {type: 'u8'}}
            }

        }
        this.data = {
            taskId: _taskId,
            networkId: _networkId,
            contractAddr: _contractAddr,
            inboundFunctionCallData: _inboundFunctionCallData
        }
    }

    toBorshBytes() {
            return borsh.serialize(this.schema, this.data);
        }
    fromBorshBytes(encoded) {
            return borsh.deserialize(this.schema, encoded);
        }
}

class EncodeProof {
    constructor(secp256k1_signagures) {
        this.schema = {
            struct:{
                signatures:{
                    array:{
                        type:{
                            struct:{
                                recid:'u8',
                                signature:{array:{type:'u8',len:64}}
                            }
                        }
                    }
                }
            }

        };
        this.data = {
            signatures: secp256k1_signagures
        };
    }
    toBorshBytes() {
        return borsh.serialize(this.schema, this.data);
    }
    fromBorshBytes(encoded) {
        return borsh.deserialize(this.schema, encoded);
    }
}

module.exports = {
    MessageData, InboundFunctionCallData, EncodeInfo, FunctionCallData, EncodeProof
};